<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>doc</title>
</head>
<body>
<script type="text/javascript">
  function ArrayList() {
    this.array = []
    ArrayList.prototype.insert = function (item) {
      this.array.push(item)
    }
    ArrayList.prototype.toString = function () {
      return this.array.join('->')
    }
    ArrayList.prototype.swap = function (m,n) {
      let temp = this.array[m]
      this.array[m] = this.array[n]
      this.array[n] = temp
    }
    ArrayList.prototype.bubbleSort = function () {
      var len = this.array.length
      for(var i = 0; i < len-1; i ++){
        for(var j = 0; j<len-1-i;j++){
          if(this.array[j] > this.array[j+1]){
            this.swap(j,j+1)
          }
        }
      }
    }
    // ArrayList.prototype.selectionSort = function () {
    //     var len = this.array.length
    //   for (let i = 0; i < len-1; i++) {
    //     var min = i
    //     for(let j = i+1;j<len;j++){
    //       if (this.array[j]<this.array[min]){
    //         min = j  //如果后一个值比默认值还小，更换较小值，这个循环遍历之后能找到该循环最小的值
    //       }
    //     }
    //     this.swap(i,min)  //将最小值与最开始默认最小值互换
    //   }
    // }

    // ArrayList.prototype.selectionSort = function () {
    //     let len = this.array.length
    //     for (let i = 0;i<len-1;i++){
    //       let min = i
    //       for (let j = i + 1;j<len;j++){
    //         if(this.array[j]<this.array[min]){
    //           min = j
    //         }
    //       }
    //       this.swap(i,min)
    //     }
    // }












    ArrayList.prototype.selectionSort = function () {
        var len = this.array.length
        for(var i = 0; i<len-1;i++){
          var min = i
          for (var j = i + 1; j < len ; j++){
            if(this.array[j]<this.array[min]){
              min = j
            }
          }
          this.swap(i,min)
        }
    }

    ArrayList.prototype.bubbleSort = function () {
        var len = this.array.length
      for (let i = 0; i < len - 1; i++) {
        for (let j = 0; j < len-1-i ; j++){
          if(this.array[j]>this.array[j+1]){
            this.swap(j,j+1)
          }
        }
      }
    }
    debounce(func,delay){
      let timer = null
      return function (...args) {
        if(timer) clearTimeout(timer)
        timer = setTimeout(()=>{
          func.apply(this,args)
        },delay)
      }
    }









  }
  let aList = new ArrayList()
  aList.insert(10)
  aList.insert(22)
  aList.insert(14)
  aList.insert(19)
  aList.insert(25)
  aList.insert(23)
  aList.insert(31)
  aList.insert(18)
  alert(aList)
  aList.bubbleSort();
  // aList.selectionSort()
  alert(aList)
</script>
</body>
</html>